<template>
  <div class="level" v-loading="loading" element-loading-text="目标标准模板下发中...">
    <!-- 搜索 -->
    <levelSearch :key="levelType" :level-type="levelType" @search="search">
      <template slot="handle">
        <el-button v-permission="{type:levelType,per:[104,111,120,128,136]}" type="success" @click="handleDialog('add')">新增</el-button>
        <el-button v-permission="{type:levelType,per:[105,113,121,129,138]}" type="primary" @click="handleDialog('copy')">复制</el-button>
        <el-button v-if="levelType !='XT_WH_XX'" v-permission="{type:levelType,per:[106,114,122,130,139]}" type="primary" @click="handleDialog('child')">新增子级模板</el-button>
      </template>
    </levelSearch>

    <!-- tabs数据 -->
    <div class="level-content">
      <cardToList :queryPermission="{type:levelType,per:[110,118,126,134,143]}" :editPermission="{type:levelType,per:[107,115,123,131,140]}" :releasePermission="{type:levelType,per:[108,116,124,132,141]}" :delPermission="{type:levelType,per:[109,117,125,133,142]}" :activeName="activeName" :tabs="tabs" :card-data="cardData" :level-type="levelType" :isEdit="true" :isRelease="true" :isDelete="true" :checkStatus="true" :isModel="true" @jumpList="jumpList" @del="del" @edit="edit" @release="release" @tabClick="tabClick" @changeId="changeId" @changePage="changePage"></cardToList>
    </div>

    <!-- 新增、编辑、复制目标标准弹框 -->
    <add-dialog v-if="addParams.visible" :levelType="levelType" :addParams="addParams" :currentId="currentId" :parentData="cardData" @getList="getList"></add-dialog>
  </div>
</template>

<script>
import { getStorage } from '../../../config/utils'
import { queryFormWorkByPage, delFormWorkData, releaseFormWork } from '@/api/target'
import addDialog from '@/views/targetSystem/targetManage/module/addLevel'
import cardToList from '@/components/cardToList/index'
import levelSearch from '@/components/search/levelSearch'

export default {
  name: 'level',
  components: {
    addDialog,
    cardToList,
    levelSearch
  },
  data () {
    return {
      loading: false,
      // 新增相关参数
      addParams: {
        visible: false,
        title: ''
      },
      // 搜索参数
      searchFrom: {
        name: '',
        dutyPart: '',
        year: '',
        rectificationRangeId: 0,
        pageIndex: 1,
        pageSize: 10
      },
      tabs: [],
      activeName: '', // 选中的tab
      // 层级：学校层，专业层，课程层，教师层，学生层
      levelType: '',
      cardData: [],
      currentId: '' // 当前选的id
    }
  },
  created () {
    // 年度默认为当前年份
    // this.searchFrom.year = new Date().getFullYear()
    this.searchFrom.year = ''
    this.levelType = this.$route.params.type
    this.setTabsByType()
  },
  methods: {
    // 新增、复制、新增子项目标标准弹框
    handleDialog (type) {
      switch (type) {
        case 'add':
          this.addParams = {
            visible: true,
            title: '添加目标标准',
            type: 'add'
          }
          break
        case 'copy':
          this.addParams = {
            visible: true,
            title: '复制目标标准',
            type: 'copy'
          }
          break
        case 'child':
          if (this.currentId) {
            this.addParams = {
              visible: true,
              title: '新增子项',
              type: 'child'
            }
          } else {
            this.$message.warning('请选择需要新增子项的目标标准')
          }
          break
      }
    },
    // 根据菜单上的层级查询对应的适用范围
    setTabsByType () {
      const list = getStorage('rangeList')
      if (list && list.length) {
        const temp = list.filter(item => item.code === this.levelType)
        if (temp.length) {
          this.tabs = temp[0].rectificationRangeDTOList

          // 默认显示第一个tabs页的数据
          this.searchFrom.rectificationRangeId = this.tabs[0].id
          this.getList()
        }
      }
    },
    // 查询
    search (searchFrom) {
      this.searchFrom.name = searchFrom.name
      this.searchFrom.dutyPart = searchFrom.dutyPart
      this.searchFrom.year = searchFrom.year
      this.getList()
    },
    // 获取列表
    getList () {
      queryFormWorkByPage(this.searchFrom)
        .then(res => {
          this.cardData = res || []
        })
        .catch()
    },
    // 跳转进入基础信息页面
    jumpList (id, data) {
      this.$router.push({
        path: `/target/standard/levelList/${this.levelType}`,
        query: {
          index: 2,
          id: id,
          detail: 'true',
          issueState: data.state,
          rectificationRangeId: data.rectificationRangeId,
          levelType: this.levelType
        }
      })
    },
    // 删除
    del (id) {
      delFormWorkData(id)
        .then(res => {
          this.$message.success('操作成功')
          this.getList()
        })
        .catch()
    },
    // 编辑
    edit (id) {
      this.$router.push({
        path: `/target/standard/levelList/${this.levelType}`,
        query: {
          index: 1,
          id: id
        }
      })
    },
    // 发布
    release (id) {
      this.$confirm('目标标准下发后不可更改!', '下发', {
        confirmButtonText: '确认下发',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          releaseFormWork(id)
            .then(res => {
              this.loading = false
              this.$message.success('操作成功')
              this.getList()
            })
            .catch(() => {
              this.loading = false
            })
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 切换tabs
    tabClick (id) {
      this.searchFrom.rectificationRangeId = +id
      this.getList()
    },
    // 选中的卡片
    changeId (id) {
      this.currentId = id
    },
    // 切换分页
    changePage (pageIndex) {
      this.searchFrom.pageIndex = pageIndex
      this.getList()
    }
  },
  watch: {
    $route (to, from) {
      // console.log(to, from);
      this.levelType = to.params.type
      this.setTabsByType()
      // this.setTabsCard(to.params.type)
      // console.log(to)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
